package com.mdd.admin.controller.order;

import com.alibaba.excel.EasyExcel;
import com.mdd.admin.listener.OrderClueListener;
import com.mdd.admin.service.order.IOrderClueService;
import com.mdd.admin.validate.order.*;
import com.mdd.admin.vo.execl.OrderClueImport;
import com.mdd.admin.vo.order.OrderClueDetailVo;
import com.mdd.admin.vo.order.OrderClueVo;
import com.mdd.common.aop.*;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.IdValidate;
import com.mdd.system.entity.SystemAuthAdmin;
import com.mdd.system.service.dict.ISettingDictDataService;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("api/orderClue")
@Api(tags = "线索管理")
public class OrderClueController {

    @Resource
    IOrderClueService iOrderClueService;

    @Resource
    ISettingDictDataService iSettingDictDataService;

    @NotPower
    @GetMapping("/list")
    @ApiOperation(value="线索列表")
    public AjaxResult<PageResult<OrderClueVo>> list(@Validated PageValidate pageValidate, @CurrentUser SystemAuthAdminInformVo systemAuthAdmin,
                                                    @Validated OrderClueSearchValidate searchValidate) {
        PageResult<OrderClueVo> list = iOrderClueService.list(pageValidate, searchValidate,systemAuthAdmin);
        return AjaxResult.success(list);
    }
    @NotPower
    @PostMapping("/detail")
    @ApiOperation(value="线索认领")
    public  AjaxResult<Object> detail(@Validated @RequestBody IdValidate idValidate) {
        OrderClueDetailVo orderClueDetailVo= iOrderClueService.detail(idValidate);
        return AjaxResult.success(orderClueDetailVo);
    }
    @NotPower
    @PostMapping("/allocation")
    @ApiOperation(value="线索分配")
    public  AjaxResult<Object> allocation( @Validated @RequestBody OrderClueClaimValidate orderClueClaimValidate,@CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
         iOrderClueService.allocation(orderClueClaimValidate,systemAuthAdmin);
        return AjaxResult.success();
    }
    @NotLogin
    @Log(title = "线索新增")
    @PostMapping("/add")
    @ApiOperation(value="线索新增")
    public AjaxResult<Object> add(@Validated @RequestBody OrderClueCreateValidate createValidate) {

        return AjaxResult.success(iOrderClueService.add(createValidate));
    }

    @NotPower
    @Log(title = "线索编辑")
    @PostMapping("/edit")
    @ApiOperation(value="线索编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody OrderClueUpdateValidate updateValidate) {
        iOrderClueService.edit(updateValidate);
        return AjaxResult.success();
    }

    @NotPower
    @Log(title = "线索删除")
    @PostMapping("/del")
    @ApiOperation(value="线索删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iOrderClueService.del(idValidate.getId());
        return AjaxResult.success();
    }


    @Log(title = "线索状态更改")
    @PostMapping("/updateStatus")
    @ApiOperation(value="线索状态更改")
    public AjaxResult<Object> updateStatus(@Validated @RequestBody OrderClueUpdateStatusValidate updateValidate,@CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        iOrderClueService.updateStatus(updateValidate, systemAuthAdmin);
        return AjaxResult.success();
    }
    @Log(title = "导入",requestType =  RequestType.File)
    @GetMapping("/import")
    @ApiOperation(value="导入")
    public AjaxResult<Object> importClue(@RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), OrderClueImport.class, new OrderClueListener(iOrderClueService,iSettingDictDataService)).sheet().headRowNumber(3).doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success();
    }
}
